<?php

namespace App\Http\Controllers\College\Web;

use App\Http\Controllers\BaseController;
use App\Service\BaseService;
use App\Service\College\CoursesService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;

class CoursesController extends BaseController
{


    /**
     * 课程列表
     * @param Request $request
     * @return JsonResponse
     */
    public function list(Request $request)
    {
        $pageSize = (int)$request->get('pageSize', 20);
        $pageNum = (int)$request->get('pageNum', 1);
        $name = (string)$request->get('name', '');
        $categoryId = (int)$request->get('categoryId', 0);
        $data = CoursesService::getCoursesList($pageSize, $pageNum, $name, $categoryId);
        return $this->success($data);
    }

    /**
     * 添加课程
     * @param Request $request
     * @return JsonResponse
     * @throws ValidationException
     * @throws \Exception
     */
    public function add(Request $request): JsonResponse
    {
        $this->validate($request, [
            'categoryId' => 'required|integer|min:0',
            'name' => 'required|string|max:64',
            'desc' => 'required|string|max:255',
            'type' => 'required|integer|between:1,2',
            'status' => 'required|integer|between:0,1',
            'coverImg' => 'required|string|max:255',
            'resource' => [
                'required',
                'array',
                function ($attribute, $value, $fail) use ($request) {
                    foreach ($request['resource'] as $v) {
                        if (!is_string($v['url'])) {
                            $fail('resource.url必须为string');
                        }
                        if (!is_string($v['type'])) {
                            $fail('resource.type必须为string');
                        }
                        if (!is_numeric($v['duration'])) {
                            $fail('resource.duration必须为数字');
                        }
                        if (!is_string($v['name'])) {
                            $fail('resource.name必须为string');
                        }
                        if (!is_numeric($v['size'])) {
                            $fail('resource.size必须为数字');
                        }
                        if (!is_string($v['time'])) {
                            $fail('resource.time必须为string');
                        }
                    }
                }
            ],
        ]);
        CoursesService::addCourses($request);
        return $this->success();
    }


    /**
     *
     * 修改课程
     *
     * @param Request $request
     * @return JsonResponse
     * @throws ValidationException
     * @throws \Exception
     */
    public function update(Request $request): JsonResponse
    {
        $this->validate($request, [
            'categoryId' => 'required|integer|min:0',
            'name' => 'required|string|max:64',
            'desc' => 'required|string|max:255',
            'type' => 'required|integer|between:1,2',
            'status' => 'required|integer|between:0,1',
            'coverImg' => 'required|string|max:255',
            'resource' => [
                'required',
                'array',
                function ($attribute, $value, $fail) use ($request) {
                    foreach ($request['resource'] as $v) {
                        if (!is_string($v['url'])) {
                            $fail('resource.url必须为string');
                        }
                        if (!is_string($v['type'])) {
                            $fail('resource.type必须为string');
                        }
                        if (!is_numeric($v['duration'])) {
                            $fail('resource.duration必须为数字');
                        }
                        if (!is_string($v['name'])) {
                            $fail('resource.name必须为string');
                        }
                        if (!is_numeric($v['size'])) {
                            $fail('resource.size必须为数字');
                        }
                        if (!is_string($v['time'])) {
                            $fail('resource.time必须为string');
                        }
                    }
                }
            ],
        ]);
        if (empty($request->id)) {
            return BaseService::throwErr("id不能为空");
        }
        CoursesService::updateCourses($request);
        return $this->success();
    }

    /**
     * get info
     * @param Request $request
     * @return JsonResponse
     */
    public function info(Request $request)
    {
        if (empty($request->id)) {
            return BaseService::throwErr("id不能为空");
        }
        $data = CoursesService::getCoursesInfo($request);
        return $this->success($data);
    }

    /**
     * 删除课程
     * @param Request $request
     * @return JsonResponse
     * @throws \Exception
     */
    public function delete(Request $request)
    {
        if (empty($request->id)) {
            return BaseService::throwErr("id不能为空");
        }
        CoursesService::deleteCourses($request);
        return $this->success();
    }
}
